package com.tengju.support.domain.model.article;

import com.tengju.support.domain.shared.DomainException;
import com.tengju.support.domain.shared.DomainExceptionCode;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Value
public class Article {

    @NonFinal
    ArticleId articleId;

    @NonFinal
    String title;

    @NonFinal
    ArticleType articleType;

    @NonFinal
    String context;

    String createBy;

    @NonFinal
    String updateBy;

    LocalDateTime createTime;

    @NonFinal
    Integer viewCnt;

    public void edit(String title, ArticleType type, String context, String operator) {
        if (!StringUtils.isEmpty(title)) {
            this.title = title;
        }
        if (type != null) {
            this.articleType = type;
        }
        if (!StringUtils.isEmpty(context)) {
            this.context = context;
        }
        if (StringUtils.isEmpty(operator)) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA, "操作人为空");
        }
        this.updateBy = operator;
    }

}
